jQuery(document).ready(function($) {
    $('#example').dataTable({
        "bJQueryUI": true,
        "iDisplayLength": 15,
        "bLengthChange": false,
        "sPaginationType": "full_numbers",
        "aoColumnDefs": [ { "bSortable": false, "aTargets": [ 2 ] } ]
    });
   
   $('#lectures').dataTable({
        "bJQueryUI": true,
        "iDisplayLength": 15,
        "bLengthChange": false,
        "sPaginationType": "full_numbers",
        "aoColumnDefs": [ { "bSortable": false, "aTargets": [ 2 ] } ]
    });
    
    $('#materials').dataTable({
        "bJQueryUI": true,
        "iDisplayLength": 15,
        "bLengthChange": false,
        "sPaginationType": "full_numbers"
    });


	$('#imp_mat').dataTable({
		"bJQueryUI" : true,
		"iDisplayLength" : 15,
		"bLengthChange" : false,
		"sPaginationType" : "full_numbers"
	});

    $("#confirm_deletion").dialog({
      autoOpen: false,
      modal: true
    });
	

	$.fn.cloneCourse = function(course_id) {
		$("#confirm_deletion").html("Do you really want to create a copy of this course and all its contents?");

		$("#confirm_deletion").dialog({
			buttons : {
				"Confirm" : function() {
					window.location = "?page=woodle_admin&action=woodle_duplicate_course&course_id=" + course_id;
				},
				"Cancel" : function() {
					$(this).dialog("close");
				}
			}
		});

		$("#confirm_deletion").dialog("open");
	};


	$.fn.editCourse = function(course_id) {
		window.location = "?page=woodle_admin&action=woodle_edit_course&course_id=" + course_id;
	}

	$.fn.editLecture = function(course_id, lecture_id) {
		window.location = "?page=woodle_admin&action=woodle_edit_lecture&course_id=" + course_id + "&lecture_id=" + lecture_id;
	}

	$.fn.addLecture = function(course_id) {
		window.location = "?page=woodle_admin&action=woodle_add_lecture&course_id=" + course_id;
	}

	$.fn.addCourse = function() {
		window.location = "?page=woodle_admin&action=woodle_add_course";
	}
   
	$.fn.deleteLecture = function(lecture_id) {
		var targetUrl = "?page=woodle_admin&action=woodle_remove_lecture";

		$("#confirm_deletion").html("Do you really want to delete this lecture?");

		$("#confirm_deletion").dialog({
			buttons : {
				"Confirm" : function() {
					$.post(targetUrl, {
						lecture_id : lecture_id
					}, function(data) {
						window.location.reload(true);
					});
				},
				"Cancel" : function() {
					$(this).dialog("close");
				}
			}
		});

		$("#confirm_deletion").dialog("open");
	};

	$.fn.deleteCourse = function(course_id) {
		var targetUrl = "?page=woodle_admin&action=woodle_remove_course";

		$("#confirm_deletion").html("Do you really want to delete this course?");

		$("#confirm_deletion").dialog({
			buttons : {
				"Confirm" : function() {
					$.post(targetUrl, {
						course_id : course_id
					}, function(data) {
						window.location.reload(true);
					});
				},
				"Cancel" : function() {
					$(this).dialog("close");
				}
			}
		});

		$("#confirm_deletion").dialog("open");
	};

	$.fn.deleteMaterial = function(course_id, lecture_id, material_id) {
		var targetUrl = "?page=woodle_admin&action=woodle_remove_material";

		$("#confirm_deletion").html("Do you really want to delete this material?");

		$("#confirm_deletion").dialog({
			buttons : {
				"Confirm" : function() {
					$.post(targetUrl, {
						material_id : material_id
					}, function(data) {
						window.location = "?page=woodle_admin&action=woodle_edit_lecture&course_id="+course_id+"&lecture_id="+lecture_id;
					});
				},
				"Cancel" : function() {
					$(this).dialog("close");
				}
			}
		});

		$("#confirm_deletion").dialog("open");
	};

	$("#lecture_date").datetimepicker({
		dateFormat: 'yy-mm-dd',
		timeFormat: 'hh:mm:ss',
		ampm: false,
		hourMin: 8,
		hourMax: 20,
		stepMinute: 15
	});
	
	$.fn.downloadFile = function(link) {
		window.open(link,'_blank');
	}
	
	
	$.fn.importMaterial = function(course_id, lecture_id) {
		var link = "?page=woodle_admin&action=woodle_import_material&course_id="+course_id+"&lecture_id="+lecture_id;
		window.location = link;
	}

	$("#imp_mat tr").live("click", function(e) {
		var $el = $(this).find("input");
		$el.prop("checked", !$el.is(':checked'));
	});
});




